package com.songshuai.webapi.permission.config;

import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.redis.RedisUtil;
import com.songshuai.webapi.common.threadpool.BaseThreadPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by yubo on 12/11/2018.
 */
@Configuration
public class BeanConfig {

    @Resource
    RedisTemplate redisTemplate;

    @Bean
    public SnowFlakeUtil snowFlakeUtil() {
        //:todo eureka dispatch id
        return new SnowFlakeUtil(2, 1);
    }

    @Bean
    public RedisUtil redisUtil() {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setSuffixKey("permission");
        redisUtil.setRedisTemplate(redisTemplate);
        return redisUtil;
    }

    @Bean
    public ConcurrentTaskScheduler concurrentTaskScheduler() {
        ConcurrentTaskScheduler scheduler = new ConcurrentTaskScheduler();
        return scheduler;
    }



    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        RestTemplate restTemplete=new RestTemplate(factory);
        return  restTemplete;
    }


    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(5000);//毫秒
        factory.setReadTimeout(5000);
        return factory;
    }

    /**
     * 默认线程池线程池
     */
    @Bean
    public ThreadPoolTaskExecutor defaultThreadPool() {
        BaseThreadPool baseThreadPool=new BaseThreadPool();
        ThreadPoolTaskExecutor executor = baseThreadPool.getThreadPoolTaskExecutor();
        return executor;
    }



}
